#!/usr/bin/env bash

MRU_SCRIPT_ROOT=$(realpath $(dirname $0))

# https://b4b4r07.hatenadiary.com/entry/2015/11/08/013526
function mru() {
	local -a f
	f=(
		~/.cache/nvim/fzf_preview/mrw(N)
		~/.cache/nvim/fzf_preview/mru(N)
		~/.vim_mru_files(N)
		~/.unite/file_mru(N)
		~/.cache/ctrlp/mru/cache.txt(N)
		~/.frill(N)
	)
	if [[ $#f -eq 0 ]]; then
		echo "There is no available MRU Vim plugins" >&2
		return 1
	fi

	local cmd q k res
	local line ok make_dir i arr
	local get_styles styles style
	local git_root
	while : ${make_dir:=0}; ok=("${ok[@]:-dummy_$RANDOM}"); cmd="$(
		cat <$f <(find . -type f -not -path '*/\.git/*') \
			| while read line; do [ -e "$line" ] && echo "$line"; done \
			| while read line; do [ "$make_dir" -eq 1 ] && echo "${line:h}/" || echo "$line"; done \
			| awk '!a[$0]++' \
			| perl -pe 's/^(\/.*\/)(.*)$/\033[34m$1\033[m$2/' \
			| $(__fzfcmd) --ansi --multi --query="$q" \
			--no-sort --exit-0 --prompt="MRU> " \
			--print-query --expect=ctrl-v,ctrl-x,ctrl-l,ctrl-q,ctrl-r,"?" \
			--height 40% \
			--keep-right \
			--preview "${MRU_SCRIPT_ROOT}/fzf-preview.sh {}" --preview-window wrap
		)"; do
		q="$(head -1 <<< "$cmd")"
		k="$(head -2 <<< "$cmd" | tail -1)"
		res="$(sed '1,2d;/^$/d' <<< "$cmd")"
		[ -z "$res" ] && continue
		case "$k" in
			"?")
				cat <<HELP > /dev/tty
usage: vim_mru_files
    list up most recently files
keybind:
  ctrl-q  output files and quit
  ctrl-l  less files under the cursor
  ctrl-v  vim files under the cursor
  ctrl-r  change view type
  ctrl-x  remove files (two-step)
HELP
				return 1
				;;
			ctrl-r)
				if [ $make_dir -eq 1 ]; then
					make_dir=0
				else
					make_dir=1
				fi
				continue
				;;
			ctrl-l)
				export LESS='-R -f -i -P ?f%f:(stdin). ?lb%lb?L/%L.. [?eEOF:?pb%pb\%..]'
				arr=("${(@f)res}")
				if [[ -d ${arr[1]} ]]; then
					ls -l "${(@f)res}" < /dev/tty | less > /dev/tty
				else
					if has "pygmentize"; then
						get_styles="from pygments.styles import get_all_styles
						styles = list(get_all_styles())
						print('\n'.join(styles))"
						styles=( $(sed -e 's/^  *//g' <<<"$get_styles" | python) )
						style=${${(M)styles:#solarized}:-default}
						export LESSOPEN="| pygmentize -O style=$style -f console256 -g %s"
					fi
					less "${(@f)res}" < /dev/tty > /dev/tty
				fi
				;;
			ctrl-x)
				if [[ ${(j: :)ok} == ${(j: :)${(@f)res}} ]]; then
					eval '${${${(M)${+commands[gomi]}#1}:+gomi}:-rm} "${(@f)res}" 2>/dev/null'
					ok=()
				else
					ok=("${(@f)res}")
				fi
				;;
			ctrl-q)
				echo "$res" < /dev/tty > /dev/tty
				return $status
				;;
			ctrl-v|*)
				git_root=$(builtin cd -q $(dirname ${(@f)res}) && git rev-parse --show-toplevel 2> /dev/null)
				if [[ -n ${git_root} ]]; then
					builtin cd -q ${git_root}
				fi
				eval $EDITOR -p "${(@f)res}" < /dev/tty > /dev/tty
				break
				;;
		esac
	done
	return
}
